

# create data available for matching
reds.small = reds %>%
  filter(!is.na(vscreserv)) %>%
  group_by(stateid, villageid, period_new) %>%
  mutate(uniq_vill_period = group_indices())

# create village level dataset
reds.vill = reds.small %>%
  group_by(uniq_vill_period) %>%
  summarise_at(vars(stateid, period_new, vscreserv, vstreserv, vscstreserv, 
                    prop_SC91, prop_SCnow, prop_ST91, prop_STnow), .funs = mean)

# create index for villages
reds.vill$index = rownames(reds.vill) %>% as.numeric()


# set up covariates to balance
X = reds.vill %>% ungroup() %>%
  dplyr::select(prop_SC91, prop_SCnow, prop_ST91, prop_STnow, 
                period_new)

# Set up list of treatments
Tr.list = list(reds.vill$vscreserv, reds.vill$vstreserv, reds.vill$vscstreserv)

# Set up matching functions
# Grab treatment and control indeces
match_index_fun = function(Tr, caliper){
  match = Match(Tr = Tr, X = X, M = 1,
                exact = c(FALSE, FALSE, FALSE, FALSE, TRUE),
                caliper = caliper,
                replace = F)
  return(c(match$index.control, match$index.treated))
}

# prepare final matched data
create_match_data = function(Tr, save.loc, caliper){
  set.seed(20200720)
  caliper = caliper
  index = reds.vill %>% 
    filter(index %in% match_index_fun(Tr = Tr, caliper = caliper)) %>%
    dplyr::select(uniq_vill_period) %>% unique %>% unlist %>% unname()
  
  subset = reds.small %>%
    filter(uniq_vill_period %in% index)
  
  write.csv(subset, save.loc)
  return(subset)
}

# create matching algorithm with optimal match
st = create_match_data(Tr = reds.vill$vstreserv, caliper = 1,
                       save.loc = "data/REDS_matched_vstreserv.csv")
scst = create_match_data(Tr = reds.vill$vscstreserv, caliper = 1,
                         save.loc = "data/REDS_matched_vscstreserv.csv")

